
<script setup lang="ts">
import { ref, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app';
import { getCategoryTopAPI } from '@/services/category';
import type { CategoryTopItem } from '@/types/category';
import type { BannerItem } from '@/types/home';
import { getHomeBannerAPI } from '@/services/home';
import Skeleton from './components/skeleton.vue'

//banner
const bannerList = ref<BannerItem[]>([])
const getBannerData = async () => {
  const res = await getHomeBannerAPI(2)
  bannerList.value = res.result
}

//获取分类列表数据
const categoryList = ref<CategoryTopItem[]>([])
const getCategoryTopData = async () => {
  const res = await getCategoryTopAPI()
  categoryList.value = res.result
}

//当前分类的下标
const activeIndex = ref(0)

const isFinish = ref(false)

onLoad(async ()=> {
  await Promise.all([getBannerData(),getCategoryTopData()])
  isFinish.value = true
})

//获取二级分类数据
const subCategoryList = computed(() => {
  return categoryList.value[activeIndex.value]?.children || []
})

</script>

<template>
  <view class="viewport" v-if="isFinish">

    <!-- 搜索 -->
    <view class="search">
      <view class="input">
        <text class="icon-search">女靴</text>
      </view>
    </view>

    <view class="categories">
      <scroll-view scroll-y class="primary">
        <view v-for="(item, index) in categoryList" :key="item.id" class="item" :class="{ active: index === activeIndex }">
          <text class="name" @tap="activeIndex = index"> {{ item.name }} </text>
        </view>
      </scroll-view>

      <scroll-view scroll-y class="secondary">
        <XtxSwiper class="banner" :list="bannerList" />
        <view class="panel" v-for="item in subCategoryList" :key="item.id">
          <view class="title">
            <text class="name">{{ item.name }}</text>
            <navigator class="more" hover-class="none">
              全部
            </navigator>
          </view>
          <view class="section">
            <navigator
              :url="`/pages/goods/goods?id=${goods.id}`"
              hover-class="none"
              class="goods"
              v-for="goods in item.goods"
              :key="goods.id"
            >
              <view class="imageView">
                <image
                class="image"
                :src="goods.picture"
              ></image>
              </view>
              <view class="name ellipsis">{{ goods.name }}</view>
              <view class="price">
                <text class="symbol">¥</text>
                <text class="number">{{ goods.price }}</text>
              </view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
  <Skeleton v-else />
</template>

<style lang="scss">

page{
  height: 100%;
  overflow: hidden;
  background-color: #f4f4f4;
}

.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.search {
  background-color: white;
  width: 100%;

  .input {
    margin: 20rpx 30rpx;
    background-color: #f1f1f1;
    height: 60rpx;
    padding-left: 30rpx;
    border-radius: 30rpx;
  }
}

.icon-search {
  line-height: 60rpx;

  &::before {
    margin-right: 10rpx;
    
  }
}

.categories {
  display: flex;
  flex: 1;
  min-height: 400rpx;
}

.primary {
  width: 180rpx;
  overflow: hidden;
  flex:none;
  .item {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 26rpx;
    background-color: #f1f1f1;
    width: 100%;
    height: 90rpx;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      left: 42rpx;
      width: 96rpx;
      bottom: 0;
      border-top: 1rpx solid #e3e4e7;
    }
  }


  .active {
      background-color: white;
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        width: 8rpx;
        height: 100%;
        background-color: #27ba9B;
      }
    }
}

.primary .item:last-child::after,
.primary .active::after {
  display: none;
}

.secondary {
  flex: 1;
  .banner {
    margin: 20rpx 30rpx;
    border-radius: 10rpx;
    overflow: hidden;
  }  

  .title {
    margin: 30rpx 30rpx;
    display: flex;
    justify-content: space-between;
    border-bottom: 1rpx solid #e3e4e7;

    .name {
      margin-bottom: 10rpx;
      font-size: 30rpx;
    }

    .more {
      color: #999;
      padding-left: 20rpx;
      font-size: 24rpx;
      &::after {
        font-family: 'erabbit' !important;
        content: '\e6c2';
      }
    }
  }

  .section {
    margin: 30rpx 30rpx;
    display: flex;
    flex-wrap: wrap;
    gap: 20rpx;
  }

  .goods {
    flex: 1;
    width: calc((100% - 40rpx) / 3.0);
    min-width: calc((100% - 40rpx) / 3.0);
    max-width: calc((100% - 40rpx) / 3.0);
    .imageView {
      width: 100%;
      padding-top: 100%;
      position: relative;

      .image {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }

    .name {
      font-size: 22rpx;
      color: #333;
      padding-top: 5rpx;
    }

    .price {
      padding:5rpx 0;
      font-size: 18rpx;
      color: #cf4444;
    }

    .number {
      font-size: 24rpx;
      margin-left: 2rpx;
    }

  }

}

</style>